package com.example.fiction.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.example.fiction.R;
import com.example.fiction.app.MyApplication;
import com.example.fiction.ui.activity.home.HomeActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

/**
 * 请求权限工具类
 * author:姚鸿杰
 * time: 2019/5/1 21:11
 */
public class PermissionUtils {

    private static final String TAG = "PermissionUtils";

    @SuppressLint("CheckResult")
    private static void applyPermission(final Activity activity) {
        RxPermissions rp = new RxPermissions(activity);
        rp.request(Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        if (aBoolean) {
                            Log.i(TAG, "accept: 权限申请成功");
                            show(activity);
                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                            builder.setTitle(R.string.app_name).setMessage(R.string.not_permission);
                            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    //跳转到权限页面
                                    JumpPermissionManagementUtils.GoToSetting(activity);
                                }
                            }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    activity.finish();
                                }
                            });
                            builder.setCancelable(false);
                            builder.create().show();
                        }
                    }
                });
    }

    public static void permissionHint(final Activity activity) {
        RxPermissions rp = new RxPermissions(activity);
        boolean storage = rp.isGranted(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        boolean readPhone = rp.isGranted(Manifest.permission.READ_PHONE_STATE);
        if (storage && readPhone) {
            //权限均申请成功
            show(activity);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
            builder.setTitle(R.string.app_name).setMessage(R.string.permission);
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    applyPermission(activity);
                    dialog.dismiss();
                }
            });
            builder.setCancelable(false);   //不可按返回键取消
            builder.create().show();
        }
    }

    private static void show(final Activity activity) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                activity.startActivity(new Intent(activity, HomeActivity.class));
                activity.finish();
            }
        }, 500);
    }
}
